// component/index/tips/worktime.js
const app = getApp();

function formatDate(value, format) {
    let parts = value.split("-");
    if (parts.length == 3) {
        let year = parts[0];
        let month = parts[1];
        let day = parts[2];
        if (format != null) {
            value = format.replace('yyyy', year).replace('MM', month).replace('dd', day);
        }
        return value;
    } else {
        return null;
    }
}

//日期选择期开始时间
function dateString(days) {
    var time = new Date().getTime() + 60 * 60 * 1000 * 24 * days;
    return new Date(time).toISOString().split('T')[0];
}


Component({
    "options": {
        styleIsolation: 'isolated'
    },
    /**
     * 组件的属性列表
     */
    properties: {
        value: {type: String, value: null},
        format: {type: String, value: 'yyyy-MM-dd'}
    },

    /**
     * 组件的初始数据
     */
    data: {
        indextips: false,
        datestr: "请选择日期",
        starttime:''
    },

    lifetimes: {
        attached() {

        },
        ready() {
            let time = dateString(3);
            this.setData({ starttime:time });
        }
    },

    observers: {
        "value": function (value) {
            if (value != null) {
                let datestr = formatDate(value, this.data.format);
                if (datestr != null) {
                    this.setData({
                        datestr: datestr
                    });
                }
            }
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onDateChange: function (e) {
            var date = e.detail.value;
            var datestr = formatDate(date, this.data.format);
            this.setData({
                value: date,
                datestr: datestr
            });
            this.triggerEvent('input', {_v: date, value: datestr})
        }
    }
})
